/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit.persistance;

import java.util.List;
import java.util.ArrayList;
import java.sql.*;

/**
 *
 * @author 시리우스
 */
public class ChangeUnitBranchDAO extends AbstractChangeUnitBranchDAO {

    @Override
    public List<ChangeUnitBranchDTO> findByChangeUnitId(String changeUnitId) throws PersistanceException {
        
        List<ChangeUnitBranchDTO> dtoArray = new ArrayList<ChangeUnitBranchDTO>();
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select CHANGEUNITID, BRANCHNAME from APP.CHANGEUNITBRANCH where CHANGEUNITID = '"+changeUnitId+"'";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                ChangeUnitBranchDTO dto = new ChangeUnitBranchDTO();
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setBranchName(rs.getString("BRANCHNAME"));
                
                dtoArray.add(dto);
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ChangeUnitBranchDAO.findByChangeUnitId: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dtoArray; 
    }

    @Override
    public List<ChangeUnitBranchDTO> findByBranchName(String branchName) throws PersistanceException {
        
        List<ChangeUnitBranchDTO> dtoArray = new ArrayList<ChangeUnitBranchDTO>();
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select CHANGEUNITID, BRANCHNAME from APP.CHANGEUNITBRANCH where BRANCHNAME = '"+branchName+"'";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                ChangeUnitBranchDTO dto = new ChangeUnitBranchDTO();
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setBranchName(rs.getString("BRANCHNAME"));
                
                dtoArray.add(dto);
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ChangeUnitBranchDAO.findByBranchName: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dtoArray; 
    }
  
    @Override
    public List<ChangeUnitBranchDTO> list() throws PersistanceException {
        
        List<ChangeUnitBranchDTO> dtoArray = new ArrayList<ChangeUnitBranchDTO>();
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select CHANGEUNITID, BRANCHNAME from APP.CHANGEUNITBRANCH ";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                ChangeUnitBranchDTO dto = new ChangeUnitBranchDTO();
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setBranchName(rs.getString("BRANCHNAME"));
                
                dtoArray.add(dto);
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ChangeUnitBranchDAO.list: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dtoArray; 
    }    
    
    @Override
    public void insert(ChangeUnitBranchDTO changeUnitBranchDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
//        String sql="insert into APP.CHANGEUNITBRANCH (CHANGEUNITID, BRANCHNAME ) " 
//                    + "values ('"+changeUnitBranchDTO.getChangeUnitId()+"','"+changeUnitBranchDTO.getBranchName()+"')";
        
        String sql="insert into APP.CHANGEUNITBRANCH (CHANGEUNITID, BRANCHNAME ) " 
                    + "values (?,?)";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1,changeUnitBranchDTO.getChangeUnitId());
            stmt.setString(2,changeUnitBranchDTO.getBranchName());
            
            stmt.executeUpdate(); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ChangeUnitBranchDAO.insert: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }
    }
 
    @Override
    public void update(ChangeUnitBranchDTO changeUnitBranchDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
        /*******  TODO
         ***  업데이트 기준이되는 WHERE 조건절 확인 해야 함
         ***************/
//        String sql="update APP.CHANGEUNITBRANCH set CHANGEUNITID= '"+ changeUnitBranchDTO.getChangeUnitId()
//                + "' ,BRANCHNAME= '"+ changeUnitBranchDTO.getBranchName()
//                + "' where CHANGEUNITID = '"+changeUnitBranchDTO.getChangeUnitId()+"'";
          
        String sql="update APP.CHANGEUNITBRANCH set CHANGEUNITID=?"
                  + " ,BRANCHNAME=?"
                  + " where CHANGEUNITID = ?";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql); 
            
            stmt.setString(1, changeUnitBranchDTO.getChangeUnitId());
            stmt.setString(2, changeUnitBranchDTO.getBranchName());
            stmt.setString(3, changeUnitBranchDTO.getChangeUnitId());
            
            stmt.executeUpdate(); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ChangeUnitBranchDAO.update: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }     
    }
 
    @Override
    public void delete(String changeUnitId) throws PersistanceException {
        Connection conn = null; 
        Statement stmt = null;  
        
        //Create SQL Statement 
        String sql="delete from APP.CHANGEUNITBRANCH "  
                   +"WHERE CHANGEUNITID = '"+changeUnitId+"'";  
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            stmt.executeUpdate(sql); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ChangeUnitBranchDAO.delete: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
    }
    
    @Override
    public void deleteByBranchName(String branchName) throws PersistanceException {
        Connection conn = null; 
        Statement stmt = null;  
        
        //Create SQL Statement 
        String sql="delete from APP.CHANGEUNITBRANCH "  
                   +"WHERE BRANCHNAME = '"+branchName+"'";  
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            stmt.executeUpdate(sql); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing deleteByBranchName.delete: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
    }
    
    @Override
    public void deleteByChangeUnitId(String changeUnitId) throws PersistanceException {
        Connection conn = null; 
        Statement stmt = null;  
        
        //Create SQL Statement 
        String sql="delete from APP.CHANGEUNITBRANCH "  
                   +"WHERE CHANGEUNITID = '"+changeUnitId+"'";  
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            stmt.executeUpdate(sql); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing deleteByBranchName.deleteByChangeUnitId: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
    }
}
